const puppeteer = require('puppeteer');
const cheerio = require("cheerio");

async function show (keyword){
  /* 1.新建一个浏览器的实例 */
  const browser = await puppeteer.launch({
      headless:false,
      defaultViewport:null
  });
  /* 2.新建一个页面 */
  const page = await browser.newPage();
  /* 3.到达某个页面 */
  await page.goto(`http://www.kuwo.cn/search/list?key=${keyword}`);
  /* 4.在页面停顿3s */
  await page.waitForTimeout(5000)
 /* 5.截图 */
  var content = await page.content();
  var $ = cheerio.load(content,{
      decodeEntities:false
  })
  var item = $(".song_item")
  var arr = []
  item.each((index,value)=>{
      var title = $(value).find(".song_name a").html()
      var artist_name = $(value).find(".song_artist span").html()
      var imgUrl = $(value).find(".song_rank img").attr("src")
      arr.push({
        imgUrl,
        title,
        artist_name
      })
  })
  /* 6.关闭浏览器 */
  await browser.close();
  return arr
}
module.exports = show